package com.resifind.www.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.resifind.www.exception.BaseException;
import com.resifind.www.exception.Resp;
import com.resifind.www.po.Browse;
import com.resifind.www.service.BrowseService;
import com.resifind.www.service.impl.BrowseServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

/**
 * @author 36069
 */
@SuppressWarnings({"unused", "RedundantThrows"})
@WebServlet("/browse/*")
public class BrowseServlet extends BaseServlet{
    private final BrowseService browseService = new BrowseServiceImpl();

    /**
     * 根据客户id查询浏览记录
     */
    public void fetchBrowse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Integer clientId = JSON.parseObject(params, int.class);
        try {
            Resp<List<Browse>> resp1 = browseService.queryBrowseByClientId(clientId);
            resp.getWriter().write(JSON.toJSONString(resp1));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMessage())));
        }
    }

    /**
     * 根据客户id和商户id添加或更新浏览记录
     */
    public void addOrUpdateBrowse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        JSONObject root = JSON.parseObject(params);
        int clientId = root.getInteger("clientId");
        int merchantId = root.getInteger("merchantId");
        try {
            Resp<String> resp1 = browseService.addOrUpdateBrowse(clientId, merchantId);
            resp.getWriter().write(JSON.toJSONString(resp1));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMessage())));
        }
    }

    /**
     * 根据客户id和商户id删除浏览记录
     */
    public void deleteBrowse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        JSONObject root = JSON.parseObject(params);
        int clientId = root.getInteger("clientId");
        int merchantId = root.getInteger("merchantId");
        try {
            Resp<String> resp1 = browseService.deleteBrowse(clientId, merchantId);
            resp.getWriter().write(JSON.toJSONString(resp1));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMessage())));
        }
    }

    /**
     * 根据客户id清空浏览记录
     */
    public void clearBrowse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Integer clientId = JSON.parseObject(params, int.class);
        try {
            Resp<String> resp1 = browseService.clearBrowse(clientId);
            resp.getWriter().write(JSON.toJSONString(resp1));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMessage())));
        }
    }
}
